<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>xmake</title>
  <link rel="icon" href="/assets/img/favicon.ico">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="description" content="Description">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link href="/assets/npm/github-markdown/github-markdown.min.css" rel="stylesheet">
  <style>
	.markdown-body {
		box-sizing: border-box;
		min-width: 200px;
		max-width: 980px;
		margin: 0 auto;
		padding: 45px;
	}

	@media (max-width: 767px) {
		.markdown-body {
			padding: 15px;
		}
	}
  </style>
</head>
<body>
<article class="markdown-body">
<h4>This is a mirror page, please see the original page: </h4><a href="https://xmake.io/#/manual/configuration_option">https://xmake.io/#/manual/configuration_option</a>
<div id="wwads-panel" class="wwads-cn wwads-vertical wwads-sticky" data-id="239" style="max-width:180px;bottom:20px;right:20px;width:200px;height:260px;background:#fff;position:fixed"></div>
</br>
    <script type="text/javascript" charset="UTF-8" src="https://cdn.wwads.cn/js/makemoney.js" async></script>
<script async type="text/javascript" src="//cdn.carbonads.com/carbon.js?serve=CE7I52QU&placement=xmakeio" id="_carbonads_js"></script>
<style>
#carbonads {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu,
  Cantarell, "Helvetica Neue", Helvetica, Arial, sans-serif;
}

#carbonads {
  display: flex;
  max-width: 330px;
  background-color: hsl(0, 0%, 98%);
  box-shadow: 0 1px 4px 1px hsla(0, 0%, 0%, .1);
}

#carbonads a {
  color: inherit;
  text-decoration: none;
}

#carbonads a:hover {
  color: inherit;
}

#carbonads span {
  position: relative;
  display: block;
  overflow: hidden;
}

#carbonads .carbon-wrap {
  display: flex;
}

.carbon-img {
  display: block;
  margin: 0;
  line-height: 1;
}

.carbon-img img {
  display: block;
}

.carbon-text {
  font-size: 13px;
  padding: 10px;
  line-height: 1.5;
  text-align: left;
}

.carbon-poweredby {
  display: block;
  padding: 8px 10px;
  background: repeating-linear-gradient(-45deg, transparent, transparent 5px, hsla(0, 0%, 0%, .025) 5px, hsla(0, 0%, 0%, .025) 10px) hsla(203, 11%, 95%, .4);
  text-align: center;
  text-transform: uppercase;
  letter-spacing: .5px;
  font-weight: 600;
  font-size: 9px;
  line-height: 1;
}
</style>
    <p>Define and set option switches. Each <code>option</code> corresponds to an option that can be used to customize the build configuration options and switch settings.</p>
<p>!> All domain interfaces except <code>target</code>, such as <code>option</code>, <code>task</code>, etc., cannot be placed in the outer global scope by default (unless some interfaces are shared with the target).<br>If you want to set the value to affect all options such as <code>option</code>, <code>task</code>, you can set it by anonymous global domain.</p>
<p>E.g:</p>
<pre><code class="lang-lua">-- Enter the anonymous global domain of the option, the settings inside will affect the test and test2 options.
option()
    add_defines("DEBUG")

option("test")
    -- ...
    -- Try to keep indented, because all settings after this are for the test option.

option("test2")
    -- ...
</code></pre>
<p>!> The <code>option</code> field can be repeatedly entered to implement separate settings. If you want to display the scope settings away from the current option, you can manually call the <a href="#option_end">option_end</a> interface.</p>
<h3 id="option">option</h3>
<h4 id="definingoptions">Defining options</h4>
<p>Define and set option switches for custom compilation configuration options, switch settings.</p>
<p>For example, define an option to enable test:</p>
<pre><code class="lang-lua">option("test")
    set_default(false)
    set_showmenu(true)
    add_defines("TEST")
</code></pre>
<p>Then associate it with the specified target:</p>
<pre><code class="lang-lua">target("demo")
    add_options("test")
</code></pre>
<p>Thus, if an option is defined, if this option is enabled, the macro definition of <code>-DTEST</code> will be automatically added when compiling the target.</p>
<pre><code class="lang-lua"># Manually enable this option
$ xmake f --test=y
$ xmake
</code></pre>
<h3 id="option_end">option_end</h3>
<h4 id="enddefinitionoption">End definition option</h4>
<p>This is an optional api that shows the departure option scope, similar to <a href="/mirror/manual/project_target.html#target_end">target_end</a>.</p>
<h3 id="optionadd_deps">option:add_deps</h3>
<h4 id="addingoptionsdepends">Adding options depends</h4>
<p>By setting the dependency, you can adjust the detection order of the options, which is generally used when the detection script is called by <a href="#optionon_check">on_check</a>.</p>
<pre><code class="lang-lua">option("small")
    set_default(true)
    on_check(function (option)
        -- ...
    end)

option("test")
    add_deps("small")
    set_default(true)
    after_check(function (option)
        if option:dep("small"):enabled() then
            option:enable(false)
        end
    end)
</code></pre>
<p>After the detection of the dependent small option is completed, the state of the option of the test is controlled by judging the state of the small option.</p>
<p>!> Since on_check will only be executed when the default value is not set, if the default value is set, the custom logic can be processed in the after_check phase.</p>
<h3 id="optionbefore_check">option:before_check</h3>
<p>Execute this script before option detection</p>
<pre><code class="lang-lua">option("zlib")
    before_check(function (option)
    end)
</code></pre>
<h3 id="optionon_check">option:on_check</h3>
<h4 id="customoptiondetectionscript">Custom Option Detection Script</h4>
<p>This script overrides the built-in option detection logic.</p>
<pre><code class="lang-lua">option("test")
    add_deps("small")
    on_check(function (option)
        if option:dep("small"):enabled() then
            option:enable(false)
        end
    end)
</code></pre>
<p>If the option that test depends on passes, disable the test option.</p>
<p>!> Only when <code>set_default</code> is not set, will the <code>on_check</code> be executed for custom option check script.</p>
<h3 id="optionafter_check">option:after_check</h3>
<p>Execute this script after option detection</p>
<p>After the option detection is complete, execute this script for some post-processing, or you can re-disable the option at this time:</p>
<pre><code class="lang-lua">option("test")
    add_deps("small")
    add_links("pthread")
    after_check(function (option)
        option:enable(false)
    end)
</code></pre>
<h3 id="optionset_values">option:set_values</h3>
<h4 id="settingthelistofoptionvalues">Setting the list of option values</h4>
<p>For the graphical menu configuration of <code>xmake f --menu</code> only, a list of option values is provided for quick selection by the user, for example:</p>
<pre><code class="lang-lua">option("test")
    set_default("b")
    set_showmenu(true)
    set_values("a", "b", "c")
</code></pre>
<p>The effect chart is as follows:</p>
<p><img src="/assets/img/manual/option_set_values.png" width="60%" /></p>
<h3 id="optionset_default">option:set_default</h3>
<h4 id="settingoptionsdefaults">Setting options defaults</h4>
<p>When the option value is not modified by the command <code>xmake f --option=[y|n}</code>, the option itself has a default value, which can be set through this interface:</p>
<pre><code class="lang-lua">option("test")
    -- This option is disabled by default
    set_default(false)
</code></pre>
<p>The value of the option supports not only the boolean type but also the string type, for example:</p>
<pre><code class="lang-lua">option("test")
    set_default("value")
</code></pre>
<table>
<thead>
<tr>
<th>Value Type</th>
<th>Description</th>
<th>Configuration</th>
</tr>
</thead>
<tbody>
<tr>
<td>boolean</td>
<td>Typically used as a parameter switch, value range: <code>true/false</code></td>
<td><code>xmake f --optionname=[y/n/yes/no/true/false]</code></td>
</tr>
<tr>
<td>string</td>
<td>can be any string, generally used for pattern judgment</td>
<td><code>xmake f --optionname=value</code></td>
</tr>
</tbody>
</table>
<p>If it is an option of the <code>boolean</code> value, it can be judged by <a href="/mirror/manual/conditions.html#has_config">has_config</a>, and the option is enabled.</p>
<p>If it is an option of type <code>string</code>, it can be used directly in built-in variables, for example:</p>
<pre><code class="lang-lua">-- define a path configuration option, using the temporary directory by default
option("rootdir")
    set_default("$(tmpdir)")
    set_showmenu(true)

target("test")
    -- add source files in the specified options directory
    add_files("$(rootdir)/*.c")
</code></pre>
<p>Among them, <code>$(rootdir)</code> is a custom option built-in variable, which can be dynamically modified by manual configuration:</p>
<pre><code class="lang-bash">$ xmake f --rootdir=~/projectdir/src
$ xmake
</code></pre>
<p>Specify a different source directory path for this <code>rootdir</code> option and compile it.</p>
<p>Detection behavior of the option:</p>
<table>
<thead>
<tr>
<th>default value</th>
<th>detection behavior</th>
</tr>
</thead>
<tbody>
<tr>
<td>No setting</td>
<td>Priority manual configuration modification, disabled by default, otherwise automatic detection, can automatically switch boolean and string type according to the type of value manually passed in</td>
</tr>
<tr>
<td>false</td>
<td>switch option, not automatic detection, disabled by default, can be manually configured to modify</td>
</tr>
<tr>
<td>true</td>
<td>switch option, not automatic detection, enabled by default, can be manually configured to modify</td>
</tr>
<tr>
<td>string type</td>
<td>no switch state, no automatic detection, can be manually configured and modified, generally used for configuration variable transfer</td>
</tr>
</tbody>
</table>
<h3 id="optionset_showmenu">option:set_showmenu</h3>
<h4 id="setwhethertoenablemenudisplay">Set whether to enable menu display</h4>
<p>If set to <code>true</code>, then this option will appear in <code>xmake f --help</code>, which can also be configured via <code>xmake f --optionname=xxx</code>, otherwise it can only be used inside <code>xmake.lua</code> , the modification cannot be configured manually.</p>
<pre><code class="lang-lua">option("test")
    set_showmenu(true)
</code></pre>
<p>After setting the menu to enable, execute <code>xmake f --help</code> to see that there is one more item in the help menu:</p>
<pre><code>Options:
    ...

    --test=TEST
</code></pre><p>!> After 2.6.8, this option is enabled by default and there is usually no need to configure it additionally.</p>
<h3 id="optionset_category">option:set_category</h3>
<h4 id="settingoptioncategoriesonlyformenudisplay">Setting option categories, only for menu display</h4>
<p>This is an optional configuration, only used in the help menu, the classification display options, the same category of options, will be displayed in the same group, so the menu looks more beautiful.</p>
<p>E.g:</p>
<pre><code class="lang-lua">option("test1")
    set_showmenu(true)
    set_category("test")

option("test2")
    set_showmenu(true)
    set_category("test")

option("demo1")
    set_showmenu(true)
    set_category("demo")

option("demo2")
    set_showmenu(true)
    set_category("demo")
</code></pre>
<p>The four options here are grouped into two groups: <code>test</code> and <code>demo</code>, and the layout shown is similar to this:</p>
<pre><code class="lang-bash">Options:
    ...

    --test1=TEST1
    --test2=TEST2

    --demo1=DEMO1
    --demo2=DEMO2
</code></pre>
<p>This interface is just to adjust the display layout, more beautiful, no other use.</p>
<p>In version 2.1.9, the hierarchical path name <code>set_category("root/submenu/submenu2")</code> can be set via category to configure the graphical menu interface of <code>xmake f --menu</code>, for example:</p>
<pre><code class="lang-lua">-- &#39;boolean&#39; option
option("test1")
    set_default(true)
    set_showmenu(true)
    set_category("root menu/test1")

-- &#39;choice&#39; option with values: "a", "b", "c"
option("test2")
    set_default("a")
    set_values("a", "b", "c")
    set_showmenu(true)
    set_category("root menu/test2")

-- &#39;string&#39; option
option("test3")
    set_default("xx")
    set_showmenu(true)
    set_category("root menu/test3/test3")

-- &#39;number&#39; option
option("test4")
    set_default(6)
    set_showmenu(true)
    set_category("root menu/test4")
</code></pre>
<p>The menu interface path structure finally displayed in the above configuration:</p>
<ul>
<li>root menu<ul>
<li>test1</li>
<li>test2</li>
<li>test3<ul>
<li>test3</li>
</ul>
</li>
<li>test4</li>
</ul>
</li>
</ul>
<p>The effect chart is as follows:</p>
<p><img src="/assets/img/manual/option_set_category.gif" width="60%" /></p>
<h3 id="optionset_description">option:set_description</h3>
<h4 id="settingmenudisplaydescription">Setting menu display description</h4>
<p>When the option menu is displayed, the description on the right is used to help the user know more clearly about the purpose of this option, for example:</p>
<pre><code class="lang-lua">option("test")
    set_default(false)
    set_showmenu(true)
    set_description("Enable or disable test")
</code></pre>
<p>The generated menu contents are as follows:</p>
<pre><code>Options:
    ...

    --test=TEST Enable or disable test (default: false)
</code></pre><p>This interface also supports multi-line display and outputs more detailed description information, such as:</p>
<pre><code class="lang-lua">option("mode")
    set_default("debug")
    set_showmenu(true)
    set_description("Set build mode",
                    " - debug",
                    " - release",
                    "-profile")
</code></pre>
<p>The generated menu contents are as follows:</p>
<pre><code>Options:
    ...

    --mode=MODE Set build mode (default: debug)
                                          - debug
                                          - release
                                          - profile
</code></pre><p>When you see this menu, the user can clearly know the specific use of the defined <code>mode</code> option and how to use it:</p>
<pre><code class="lang-bash">$ xmake f --mode=release
</code></pre>
<h3 id="optionadd_links">option:add_links</h3>
<h4 id="addlinklibrarydetection">Add Link Library Detection</h4>
<p>If the specified link library is passed, this option will be enabled and the associated target will automatically be added to this link, for example:</p>
<pre><code class="lang-lua">option("pthread")
    add_links("pthread")
    add_linkdirs("/usr/local/lib")

target("test")
    add_options("pthread")
</code></pre>
<p>If the test passes, the <code>test</code> target will be automatically added when it is compiled: <code>-L/usr/local/lib -lpthread</code> compile option</p>
<h3 id="optionadd_linkdirs">option:add_linkdirs</h3>
<h4 id="addingthesearchdirectoryneededforlinklibrarydetection">Adding the search directory needed for link library detection</h4>
<p>This is optional. Generally, the system library does not need to add this, and it can also pass the test. If it is not found, you can add the search directory yourself to improve the detection pass rate. For details, see: <a href="#optionadd_links">add_links</a></p>
<h3 id="optionadd_rpathdirs">option:add_rpathdirs</h3>
<h4 id="addingaloadsearchdirectoryforadynamiclibraryatruntime">Adding a load search directory for a dynamic library at runtime</h4>
<p>After the option passes the detection, it will be automatically added to the corresponding target. For details, see: <a href="/mirror/manual/project_target.html#targetadd_rpathdirs">target.add_rpathdirs</a>.</p>
<h3 id="optionadd_cincludes">option:add_cincludes</h3>
<h4 id="addcheaderfiledetection">Add c header file detection</h4>
<p>This option will be enabled if the c header file is passed, for example:</p>
<pre><code class="lang-lua">option("pthread")
    add_cincludes("pthread.h")
    add_defines("ENABLE_PTHREAD")

target("test")
    add_options("pthread")
</code></pre>
<p>This option checks if there is a <code>pthread.h</code> header file. If the test passes, then the <code>test</code> target program will add the macro definition of <code>ENABLE_PTHREAD</code>.</p>
<p>If you want more flexible detection, you can do this in <a href="#optionon_check">option.on_check</a> via <a href="#detect-has_cincludes">lib.detect.has_cincludes</a>.</p>
<h3 id="optionadd_cxxincludes">option:add_cxxincludes</h3>
<h4 id="addcheaderfiledetection">Add c++ header file detection</h4>
<p>Similar to <a href="#optionadd_cincludes">add_cincludes</a>, except that the detected header file type is a c++ header file.</p>
<h3 id="optionadd_ctypes">option:add_ctypes</h3>
<h4 id="addctypedetection">Add c type detection</h4>
<p>This option will be enabled if the c type is passed, for example:</p>
<pre><code class="lang-lua">option("wchar")
    add_ctypes("wchar_t")
    add_defines("HAVE_WCHAR")

target("test")
    add_options("wchar")
</code></pre>
<p>This option checks if there is a type of <code>wchar_t</code>. If the test passes, then the <code>test</code> target program will add the macro definition of <code>HAVE_WCHAR</code>.</p>
<p>If you want more flexible detection, you can do this in <a href="#optionon_check">option.on_check</a> via <a href="#detect-has_ctypes">lib.detect.has_ctypes</a>.</p>
<h3 id="optionadd_cxxtypes">option:add_cxxtypes</h3>
<h4 id="addingctypedetection">Adding c++ type detection</h4>
<p>Similar to <a href="#optionadd_ctypes">add_ctypes</a>, except that the type detected is a c++ type.</p>
<h3 id="optionadd_csnippets">option:add_csnippets</h3>
<h4 id="addccodefragmentdetection">Add c code fragment detection</h4>
<p>If the existing <a href="#optionadd_ctypes">add_ctypes</a>, <a href="#optionadd_cfuncs">add_cfuncs</a>, etc. cannot meet the current detection requirements,<br>You can use this interface to implement more custom detection of some compiler feature detection, see: <a href="#optionadd_cxxsnippets">add_cxxsnippets</a>.</p>
<h3 id="optionadd_cxxsnippets">option:add_cxxsnippets</h3>
<h4 id="addingccodesnippetdetection">Adding c++ code snippet detection</h4>
<p>This interface can be used to implement more custom detection of some compiler feature detection, especially the detection support of various features of C++, such as:</p>
<pre><code class="lang-lua">option("constexpr")
    add_cxxsnippets("constexpr", "constexpr int f(int x) { int sum=0; for (int i=0; i<=x; ++i) sum += i; return sum; } constexpr int x = f (5); static_assert(x == 15);")
</code></pre>
<p>The first parameter sets the name of the code snippet as a label, and is displayed when the output information is detected.</p>
<p>The above code implements the detection of the constexpr feature of C++. If the test passes, the constexpr option is enabled. Of course, this is just an example.</p>
<p>For the detection of compiler features, there is a more convenient and efficient detection module, providing more powerful detection support, see: <a href="#compiler-has_features">compiler.has_features</a> and <a href="#detect-check_cxsnippets">detect.check_cxsnippets</a></p>
<p>If you want more flexible detection, you can do this in <a href="#optionon_check">option.on_check</a> via <a href="#detect-check_cxsnippets">lib.detect.check_cxsnippets</a>.</p>
<p>After v2.5.7, two new options, <code>{tryrun = true}</code> and <code>{output = true}</code>, are added to try to run detection and capture output.</p>
<p>Setting tryrun can try to run to detect:</p>
<pre><code class="lang-lua">option("test")
     add_cxxsnippets("HAS_INT_4", "return (sizeof(int) == 4)? 0: -1;", {tryrun = true})
</code></pre>
<p>Setting output will also try to detect and additionally capture the output content of the run.</p>
<pre><code class="lang-lua">option("test")
     add_cxxsnippets("INT_SIZE",&#39;printf("%d", sizeof(int)); return 0;&#39;, {output = true, number = true})
</code></pre>
<p>!> Set to capture output, the current option cannot set other snippets</p>
<p>We can also get the output bound to the option through <code>is_config</code>.</p>
<pre><code class="lang-lua">if is_config("test", "8") tben
     - xxx
end
</code></pre>
<h3 id="optionadd_cfuncs">option:add_cfuncs</h3>
<h4 id="addclibraryfunctiondetection">Add c library function detection</h4>
<pre><code class="lang-lua">option("setjmp")
    add_cincludes("setjmp.h")
    add_cfuncs("sigsetjmp", "setjmp")
    add_defines("HAVE_SETJMP")

target("test")
    add_options("setjmp")
</code></pre>
<p>This option detects whether there are some interfaces of <code>setjmp</code>. If the test passes, the target program of <code>test</code> will add the macro definition of <code>HAVE_SETJMP</code>.</p>
<p>The function fragments inside support the following syntax formats:</p>
<pre><code class="lang-lua">-- Simply detect whether the function address exists, and internally will try to determine its address
sigsetjmp

-- If some functions are defined by macro wrap, the detection can be bypassed in this way
sigsetjmp((void*)0, 0)

-- You can also specify a complete function statement, for example: funcname{codebody}
sigsetjmp{sigsetjmp((void*)0, 0);}
sigsetjmp{int a = 0; sigsetjmp((void*)a, a);}
</code></pre>
<p>Note that the detected function usually needs to be accompanied by <code>add_cincludes</code> to ensure that the function can be included normally, otherwise the detection will fail.</p>
<h3 id="optionadd_cxxfuncs">option:add_cxxfuncs</h3>
<h4 id="addclibraryfunctiondetection">Add c++ library function detection</h4>
<p>The usage is consistent with <a href="#optionadd_cxxfuncs">option:add_cfuncs</a>.</p>
</article>
</body>
</html>